#! /usr/bin/python3
# -*- coding: UTF-8 -*-

if __name__ == "__main__":
	raise Exception("不支持从这里调用")

__version__ = "1.0"
__all__ = []

from uuid import uuid4

from com.mousechsh.business.lib.MouseChshAuthorizationAnnotition import MouseChshRemoteAuthorizationAnnotition
from com.mousechsh.business.lib.model.MouseChshTemplateModel import MouseChshTemplateModel
from com.mousechsh.common.data.MouseChshDao import MouseChshDao
from com.mousechsh.common.data.MouseChshData import mousechsh_data_merge
from com.mousechsh.common.data.json.MouseChshJson import mousechsh_json_to_json
from com.mousechsh.common.io.db.MouseChshPostgresql import MouseChshPostgresql
from com.mousechsh.common.middle.http.MouseChshHttpHeader import mousechsh_http_header_accept_language_annotation
from com.mousechsh.common.middle.rest_api.MouseChshRestApi import mousechsh_rest_api_annotation
from com.mousechsh.common.middle.router.MouseChshCorsAnnotition import mousechsh_cors_annotation
from com.mousechsh.common.middle.router.MouseChshHttpRouter import mousechsh_http_router_annotation
from com.mousechsh.common.util.MouseChshDateTime import MouseChshDateTime


@mousechsh_http_router_annotation(method=['POST', 'OPTIONS'], path='/template/add')
@mousechsh_cors_annotation()
@mousechsh_http_header_accept_language_annotation()
@MouseChshRemoteAuthorizationAnnotition()
@mousechsh_rest_api_annotation()
def _addTemplate(params):
	lang = params.get('lang', 'zh-cn')
	template = MouseChshTemplateModel()
	template.set_data(params)
	template.Id = str(uuid4()).upper()
	db = MouseChshPostgresql()
	db.connect()
	db.execute(
		'''
	   INSERT INTO "A_Message"."T_Template"(
		   "C_Id",
		   "C_CrOn",
		   "C_MoOn",
		   "L_Scheme",
		   "X_Data",
		   "C_Code",
		   "L_TemplateType",
		   "X_Title",
		   "X_Content",
		   "X_Params"
	   )VALUES(
		   %(id)s,
		   %(cron)s,
		   %(moon)s,
		   'Template',
		   '{}',
		   %(code)s,
		   %(type)s,
		   %(title)s,
		   %(content)s,
		   %(params)s
	   )
		   ''', {
			'id': template.get('Id'),
			'cron': MouseChshDateTime().to_timestamp(),
			'moon': MouseChshDateTime().to_timestamp(),
			'code': template.get('Code'),
			'type': template.get('Type'),
			'title': mousechsh_json_to_json(
				{
					lang: template.get('Title'),
					'': lang
				}
			),
			'content': mousechsh_json_to_json(
				{
					lang: template.get('Content'),
					'': lang
				}
			),
			'params': mousechsh_json_to_json(template.get('Params')) if template.get('Params') else '[]'
		}
	)
	db.commit()
	db.close()
	return template.get('Id')


@mousechsh_http_router_annotation(method=['POST', 'OPTIONS'], path='/template/remove')
@mousechsh_cors_annotation()
@mousechsh_http_header_accept_language_annotation()
@MouseChshRemoteAuthorizationAnnotition()
@mousechsh_rest_api_annotation()
def _removeTemplate(params):
	template = MouseChshTemplateModel()
	template.set_data(params)
	db = MouseChshPostgresql()
	db.connect()
	db.execute(
		'''
	   DELETE FROM "A_Message"."T_Template"
	   WHERE "C_Id" = %(id)s
		   ''', {
			'id': template.get('Id')
		}
	)
	db.commit()
	db.close()
	return True


@mousechsh_http_router_annotation(method=['POST', 'OPTIONS'], path='/template/update')
@mousechsh_cors_annotation()
@mousechsh_http_header_accept_language_annotation()
@MouseChshRemoteAuthorizationAnnotition()
@mousechsh_rest_api_annotation()
def _updateTemplate(params):
	lang = params.get('lang', 'zh-cn')
	template = MouseChshTemplateModel()
	template.set_data(params)
	db = MouseChshPostgresql()
	db.connect()
	data = db.get(
		'''
	   SELECT
		   "C_Id",
		   "C_CrOn",
		   "C_CrBy",
		   "C_MoOn",
		   "C_MoBy",
		   "C_RvOn",
		   "C_RvBy",
		   "C_St",
		   "C_Owner",
		   "C_Flag",
		   "L_Scheme",
		   "X_Data",
		   "X_Property",
		   "C_Code",
		   "L_TemplateType" AS "C_TemplateType",
		   COALESCE( "X_Title" ->> %(lang)s, "X_Title" ->> ( "X_Title" ->> '' ) ) AS "C_Title",
		   "X_Title",
		   COALESCE( "X_Content" ->> %(lang)s, "X_Content" ->> ( "X_Content" ->> '' ) ) AS "C_Content",
		   "X_Content",
		   "X_Params"
	   FROM "A_Message"."T_Template"
	   WHERE "C_Id" = %(id)s
		   ''', {
			'lang': lang,
			'id': template.get('Id')
		}
	)
	db.close()
	dao = MouseChshDao()
	dao.set_model(MouseChshTemplateModel)
	templates = dao.parse(data)
	if len(templates) <= 0:
		return
	template = mousechsh_data_merge(templates[0], template)
	db = MouseChshPostgresql()
	db.connect()
	db.execute(
		'''
	   UPDATE "A_Message"."T_Template" SET
		   "C_MoOn"    = %(moon)s,
		   "L_Scheme"  = 'Template',
		   "C_Code"    = %(code)s,
		   "X_Title"   = %(title)s,
		   "X_Content" = %(content)s,
		   "X_Params"  = %(params)s
	   WHERE "C_Id"    = %(id)s
		   ''', {
			'moon': MouseChshDateTime().to_timestamp(),
			'code': template.get('Code'),
			'type': template.get('Type'),
			'title': mousechsh_json_to_json(
				{
					lang: template.get('Title'),
					'': lang
				}
			),
			'content': mousechsh_json_to_json(
				{
					lang: template.get('Content'),
					'': lang
				}
			),
			'params': mousechsh_json_to_json(template.get('Params')) if template.get('Params') else '[]',
			'id': template.get('Id')
		}
	)
	db.commit()
	db.close()
	return template.get('Id')


@mousechsh_http_router_annotation(method=['POST', 'OPTIONS'], path='/template/get')
@mousechsh_cors_annotation()
@mousechsh_http_header_accept_language_annotation()
@MouseChshRemoteAuthorizationAnnotition()
@mousechsh_rest_api_annotation()
def _getTemplate(params):
	lang = params.get('lang', 'zh-cn')
	db = MouseChshPostgresql()
	db.connect()
	data = db.get(
		'''
	   SELECT
		   A."C_Id",
		   A."C_CrOn",
		   A."C_CrBy",
		   A."C_MoOn",
		   A."C_MoBy",
		   A."C_RvOn",
		   A."C_RvBy",
		   A."C_St",
		   A."C_Owner",
		   A."C_Flag",
		   A."L_Scheme",
		   A."X_Data",
		   A."X_Property",
		   A."C_Code",
		   A."L_TemplateType" AS "C_TemplateType",
		   A."L_TemplateType" AS "L_TemplateType_Key",
		   COALESCE( B."X_Display" ->> %(lang)s, B."X_Display" ->> ( B."X_Display" ->> '' ) ) AS "L_TemplateType_Display",
		   COALESCE( A."X_Title" ->> %(lang)s, A."X_Title" ->> ( A."X_Title" ->> '' ) ) AS "C_Title",
		   A."X_Title",
		   COALESCE( "X_Content" ->> %(lang)s, "X_Content" ->> ( "X_Content" ->> '' ) ) AS "C_Content",
		   A."X_Content",
		   A."X_Params"
	   FROM "A_Message"."T_Template" A
	   LEFT OUTER JOIN "A_Const"."W_KeyValue" B
		   ON A."L_TemplateType" = B."C_Key"
	   ORDER BY A."C_Order" ASC
	   LIMIT 100
		   ''', {
			'lang': lang
		}
	)
	db.close()
	dao = MouseChshDao()
	dao.set_model(MouseChshTemplateModel)
	templates = dao.parse(data)
	return templates
